import { useState, useEffect } from 'react'
import { $get } from '@js/authRequest'
import { awaitWrap } from '@js/tool'

async function getRequest (caseid) {
  const res = await $get(`/api/xapc/case/${caseid}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

async function initData (caseid, setData) {
  const [e, d] = await awaitWrap(getRequest(caseid))
  if (e === null && d !== null) {
    setData(d)
  }
}

export function useCaseDetail (id) {
  const [data, setData] = useState(null)
  useEffect(() => {
    initData(id, setData)
  }, [id])
  return data
}
